package api

import (
	"admin100/app/model"
	"admin100/app/model/gamemodel"
	"admin100/app/service"
	"admin100/app/service/centerservice"
	"admin100/app/service/commonservice"
	"admin100/app/service/gameservice"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var LogMgr logMgrApi

type logMgrApi struct{}

// @summary 登录日志
// @tags 游戏日志
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   ip formData  string false "ip"
// @param   PlayerName formData  string false "玩家名"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/player-login [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) PlayerLogin(r *ghttp.Request) {

	var req gamemodel.PlayerLoginLogListReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	data, total := gameservice.Log.List(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 在线日志
// @tags 游戏日志
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   playerName formData  string false "玩家名"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/online-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) OnlineList(r *ghttp.Request) {
	var req gamemodel.PlayerOnlineLogListReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	data, total := gameservice.Log.OnlineList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 道具日志
// @tags 游戏日志
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   playerName formData  string true "玩家名"
// @param   propType formData  string false "道具类型"
// @param   propId formData  string false "道具id"
// @param   OpType formData  string false "操作类型"
// @param   dateTime formData  string true "日期"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/prop-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) PropList(r *ghttp.Request) {
	var req gameservice.PropLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
	player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
	req.PlayerId = int(player.Id)

	data, total := gameservice.Log.GetPlayerPropLogList(sNode, &req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 属性日志
// @tags 游戏日志
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   playerName formData  string true "玩家名"
// @param   functionId formData  string false "功能ID"
// @param   type formData  string false "操作类型"
// @param   dateTime formData  string true "日期"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/attr [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) Attr(r *ghttp.Request) {
	var req gameservice.PlayerAttrLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
	player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
	req.PlayerId = int(player.Id)

	data, total := gameservice.Log.GetPlayerAttrLogList(sNode, &req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 邮件日志
// @tags 游戏日志
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   playerName formData  string true "玩家名"
// @param   logType formData  string false "操作类型"
// @param   dateTime formData  string true "日期"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/mail [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) Mail(r *ghttp.Request) {
	var req gameservice.PlayerMailLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
	player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
	req.PlayerId = int(player.Id)

	data, total := gameservice.Log.GetPlayerMailLogList(sNode, &req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 活动奖励日志
// @tags 游戏日志
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   playerName formData  string true "玩家名"
// @param   activityId formData  string false "活动ID"
// @param   serverType formData  string false "服务器类型"
// @param   dateTime formData  string true "日期"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/activity-award [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) ActivityAward(r *ghttp.Request) {
	var req gameservice.ActivityAwardLogReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
	sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
	player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
	req.PlayerId = int(player.Id)

	data, total := gameservice.Log.GetActivityAwardLogList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 冲榜数据列表
// @tags 工具
// @produce json
// @param   platformId formData  string true "平台id"
// @param   serverId formData  string true "区服id"
// @param   playerName formData  string false "角色名"
// @param   activityId formData  string false "活动ID"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/impact-rank-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) ImpactRankList(r *ghttp.Request) {
	var req gamemodel.ImpactRankReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	if req.PlayerName != "" {
		serverId, _ := commonservice.GamePlayer.SplitPlayerName(req.PlayerName)
		sNode := centerservice.CGameServer.FetchNodeStrByPtIdAndSid(req.PlatformId, serverId)
		player := commonservice.GamePlayer.GetPlayerByPlatformIdAndNickname(sNode, req.PlayerName)
		req.PlayerId = int(player.Id)
	}

	data, total := service.Tool.GetImpactRankList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 登录日志列表
// @tags 设置
// @produce json
// @param   account formData  string false "帐号"
// @param   ip formData  string false "id"
// @param   State formData  string false "状态"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /log/login-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *logMgrApi) LoginList(r *ghttp.Request) {
	var req model.LoginAdminReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	data, total := service.User.FetchLoginAdminLogList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

//
// @summary 请求日志列表
// @tags 设置
// @produce json
// @param   account formData  string false "帐号"
// @param   Url formData  string false "id"
// @param   UserId formData  string false "状态"
// @param   LoginType formData  string false "状态"
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @param   startTime formData  string false "开始时间"
// @param   endTime formData  string false "结束时间"
// @router  /log/request-list [POST]
func (a *logMgrApi) RequestList(r *ghttp.Request) {
	var req model.RequestAdminReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	data, total := service.User.FetchRequestLogList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}
